package com.bang.concurrency.example.blq;

import org.springframework.boot.autoconfigure.kafka.KafkaProperties;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @ClassName: BlockingQueueExample
 * @Desc: 阻塞队列
 * @URL:
 *        https://www.cnblogs.com/jackyuj/archive/2010/11/24/1886553.html
 *        https://www.jianshu.com/p/024a36b83099
 * @auther: BANG
 * @Date: 2019/10/09 10:45
 * @Version: 1.0
 * @Modified By:
 */
public class BlockingQueueExample {

    public static void main(String[] args) throws Exception {
        ExecutorService service = Executors.newCachedThreadPool();

        BlockingQueue<String> blockingQueue = new LinkedBlockingQueue();

        Producer producer1 = new Producer(blockingQueue);
        Producer producer2 = new Producer(blockingQueue);
        Producer producer3 = new Producer(blockingQueue);
        Consumer consumer = new Consumer(blockingQueue);

        // 启动线程
        service.execute(producer1);
        service.execute(producer2);
        service.execute(producer3);
        service.execute(consumer);

        // 执行10s
        Thread.sleep(10 * 1000);
        producer1.stop();
        producer2.stop();
        producer3.stop();

        Thread.sleep(2000);
        // 退出Executor
        service.shutdown();

    }
}
